package com.zhihuiyanche.service.orders.impl;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhihuiyanche.dao.OrdersMapper;
import com.zhihuiyanche.domain.Orders;
import com.zhihuiyanche.domain.OrdersExample;
import com.zhihuiyanche.service.orders.OrdersService;

@Component("ordersService")
public class OrdersServiceImpl implements OrdersService
{

	@Autowired
	private OrdersMapper	ordersMapper;

	@Override
	public JSONArray getOrdersByCondition(JSONObject orders)
	{

		JSONArray ordersList = ordersMapper.getOrdersByCondition(orders);
		
		return ordersList;
	}

	@Override
	public boolean saveOrdersSuccess(Orders orders)
	{
		OrdersExample example = new OrdersExample();
		example.createCriteria().andIdEqualTo(orders.getId());
		
		ordersMapper.updateByExampleSelective(orders, example);
		return true;
	}
	
	@Override
	public List<String[]> getOrdersChart(String shopId) {
		List<String> orderDates = ordersMapper.getOrderDateDesc(shopId);
		if(orderDates.size()<1){
			return new ArrayList<String[]>();
		}else{
			int orderDatesSize = orderDates.size() > 5 ? 5:orderDates.size();
			String[] orders = new String[orderDatesSize];
			String[] dates = new String[orderDatesSize];
			for(int i = 0;i<orderDatesSize;i++){
				JSONObject data = new JSONObject();
				data.put("orderDate", orderDates.get(i));
				data.put("shopId", shopId);
				orders[i] = ordersMapper.getOrderDateCount(data);
				dates[i] = orderDates.get(i);
			}
			List<String[]> datas = new ArrayList<String[]>();
			datas.add(dates);
			datas.add(orders);
			return datas;
		}
	}


	@Override
	public String insertOrders(Orders orders)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean deleteOrdersById(Orders orders)
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteOrdersByCondition(Orders orders)
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateOrdersById(Orders orders)
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateOrdersByCondition(Orders record, Orders parameter)
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Orders getOrdersById(Orders orders)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getOrdersByPageCount(JSONObject orders)
	{
		return ordersMapper.getOrdersCountByCondition(orders);
	}
	
	public List<Orders> queryOrdersByCondition(Orders orders){
		return ordersMapper.queryOrdersByCondition(orders);
	}

	
}
